package com.dynamic.mybatis.core.generator;


import com.baomidou.mybatisplus.core.enums.SqlMethod;
import com.dynamic.mybatis.core.enums.CommandType;
import com.dynamic.mybatis.core.enums.ReturnType;
import org.apache.ibatis.mapping.SqlCommandType;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description
 * @Author xs
 * @Date 2023/3/17 18:11
 */
public interface MapperMethod {
    String name();

    String getSqlMethod();

    ReturnType getReturnType();

    String getResultType();

    CommandType getCommandType();

    String getRemarks();

    boolean isBatch();

    String getMethod();


    enum StandardMapperMethod implements MapperMethod {
        //sqlMethod @see  com.baomidou.mybatisplus.core.enums.SqlMethod
        insert(SqlMethod.INSERT_ONE.getMethod(), CommandType.StandardCommand.INSERT, null, ReturnType.StandardReturnType.BOOLEAN, Boolean.FALSE, "新增%s", "POST"),
        insertBatch(SqlMethod.INSERT_ONE.getMethod(), CommandType.StandardCommand.INSERT, null, ReturnType.StandardReturnType.BOOLEAN, Boolean.TRUE, "批量新增%s", "POST"),
        updateById(SqlMethod.UPDATE_BY_ID.getMethod(), CommandType.StandardCommand.UPDATE, null, ReturnType.StandardReturnType.BOOLEAN, Boolean.FALSE, "修改%s", "POST"),
        update(SqlMethod.UPDATE.getMethod(), CommandType.StandardCommand.UPDATE, null, ReturnType.StandardReturnType.BOOLEAN, Boolean.FALSE, "修改%s", "POST"),
        updateBatch(SqlMethod.UPDATE.getMethod(), CommandType.StandardCommand.UPDATE, null, ReturnType.StandardReturnType.BOOLEAN, Boolean.TRUE, "批量修改%s", "POST"),
        deleteById(SqlMethod.DELETE_BY_ID.getMethod(), CommandType.StandardCommand.DELETE, null, ReturnType.StandardReturnType.BOOLEAN, Boolean.FALSE, "删除%s", "POST"),
        selectById(SqlMethod.SELECT_BY_ID.getMethod(), CommandType.StandardCommand.SELECT, "map", ReturnType.StandardReturnType.MAP, Boolean.FALSE, "查询%s", "GET"),
        selectList(SqlMethod.SELECT_LIST.getMethod(), CommandType.StandardCommand.SELECT, "map", ReturnType.StandardReturnType.COLLECT, Boolean.FALSE, "查询列表%s", "GET"),
        selectPage(SqlMethod.SELECT_PAGE.getMethod(), CommandType.StandardCommand.SELECT, "map", ReturnType.StandardReturnType.PAGE, Boolean.FALSE, "分页查询%s", "GET"),
        save("save", CommandType.StandardCommand.UPDATE, null, ReturnType.StandardReturnType.BOOLEAN, Boolean.FALSE, "保存%s", "POST"),
        saveBatch("saveBatch", CommandType.StandardCommand.UPDATE, null, ReturnType.StandardReturnType.BOOLEAN, Boolean.TRUE, "批量保存%s", "POST"),
        //   upsert("upsert",SqlCommandType.INSERT,ReturnType.NUMBER,"新增或修改%s"),
        ;

        StandardMapperMethod(String sqlMethod, CommandType commandType, String resultType, ReturnType returnType, boolean batch, String remarks, String method) {
            this.sqlMethod = sqlMethod;
            this.commandType = commandType;
            this.returnType = returnType;
            this.remarks = remarks;
            this.resultType = resultType;
            this.batch = batch;
            this.method = method;
        }

        public String sqlMethod;
        public CommandType commandType;
        public ReturnType returnType;
        public String remarks;
        public String resultType;
        public boolean batch;
        public String method;

        public String getSqlMethod() {
            return sqlMethod;
        }

        public ReturnType getReturnType() {
            return returnType;
        }

        public String getRemarks() {
            return remarks;
        }

        public String getResultType() {
            return resultType;
        }

        public boolean isBatch() {
            return batch;
        }

        public CommandType getCommandType() {
            return commandType;
        }

        public String getMethod() {
            return method;
        }
    }


    static StandardMapperMethod[] values() {
        return MapperMethod.StandardMapperMethod.values();
    }

    static MapperMethod get(String method) {
        for (MapperMethod mapperMethod : MapperMethod.StandardMapperMethod.values()) {
            if (mapperMethod.name().equals(method)) {
                return mapperMethod;
            }
        }
        return null;
    }

    static List<MapperMethod> get(List<String> methods) {
        List<MapperMethod> methodList = new ArrayList<>(methods.size());
        for (MapperMethod mapperMethod : MapperMethod.StandardMapperMethod.values()) {
            if (methods.contains(mapperMethod.name())) {
                methodList.add(mapperMethod);
            }
        }
        return methodList;
    }


}